package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.PageRequest;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserLog;
import com.digital.welcome.mapper.UserLogMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserLogService;
import com.digital.welcome.util.DataEncode;
import com.digital.welcome.util.IpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class UserLogServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implements UserLogService {
    @Autowired
    private UserLogMapper userLogMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DataEncode dataEncode;

    @Override
    public void insertUserLog(User user, HttpServletRequest request) {
        User newUser = userMapper.getUserByUsername(dataEncode.decodeBae64(user.getUserName()));
        String ipAddress = IpUtil.getIPAddress(request);
        LocalDateTime localTime = LocalDateTime.now();
        UserLog userLog = new UserLog();
        userLog.setUid(newUser.getId());
        userLog.setLoginIp(ipAddress);
        userLog.setUserName(newUser.getUserName());
        userLog.setCreateTime(localTime);
        userLog.setName(newUser.getName());
        userLogMapper.insert(userLog);
    }


    @Override
    public String findPage(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<UserLog> userLogList = userLogMapper.selectList(null);
        PageInfo<UserLog> pageInfo = new PageInfo<>(userLogList);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("查询成功").setData(pageInfo));
    }

    @Override
    public String delete(UserLog userLog) {
        if (userLog == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("信息错误"));
        }
        userLogMapper.deleteById(userLog.getId());
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("用户日志删除成功"));
    }

    @Override
    public String selectLog(PageRequest pageRequest) {
        if (pageRequest == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("信息错误"));
        }
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("create_time", pageRequest.getLike());
        List<UserLog> userLogList = userLogMapper.selectList(queryWrapper);
        PageInfo<UserLog> pageInfo = new PageInfo<>(userLogList);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("查询成功").setData(pageInfo));
    }
}
